#pragma once
#include <iostream>
#include <set>
#include "InetAddr.hpp"

class Command
{
public:
    Command()
    {
        _WhiteListCommands.insert("ls");
        _WhiteListCommands.insert("pwd");
        _WhiteListCommands.insert("who");
        _WhiteListCommands.insert("whoami");
    }

    bool ISWHITE(const std:: string& cmd)
    {
        auto iter = _WhiteListCommands.find(cmd);
        return iter != _WhiteListCommands.end();
    }
    std::string Execute(const std:: string& cmd, Inet_Addr& addr)
    {
        // if(!ISWHITE(cmd))
        // {
        //     return std::string("非白名单指令");
        // }

        //获取操控人的名字
        std::string who = addr.StringAddr();

        FILE* fp = popen(cmd.c_str(), "r");
        if(nullptr == fp)
        {
            return std::string("你执行的命令不存在") + cmd;
        }

        std::string res;
        char line[1024];
        while(fgets(line, sizeof line, fp))
        {
            res += line;
        }
        res = who + "execute done, result is: \n" + res;
        return res;
    }
    ~Command()
    {

    }
private:
    std::set<std::string> _WhiteListCommands;
};